const seneca = require('./shared-seneca');
const genPins = require('../gen-pins');
const uuidv1 = require('uuid/v1');

async function setup(options = { dontEnroll: false, serviceUuid }) {
    let { dontEnroll, serviceUuid, listen } = options;
    if(!serviceUuid) {
        serviceUuid = options.serviceUuid || uuidv1();
    }
    options.plugins.forEach(plugin => {
        seneca.use(plugin, { serviceUuid });
    });
    let { pin } = listen;
    let pins = genPins(pin);
    // seneca.listen({ pins });
    await waitSenecaReady(seneca);
    return this;
}

function assertInvoke(args) {
    if (!seneca) {
        throw new Error('should invoke setup before got seneca service-host!');
    }
}

function waitSenecaReady(seneca) {
    return new Promise((resolve, reject) => {
        seneca.ready(() => {
            resolve();
        });
    });
}

module.exports = {
    setup
};